﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace CommandLib
{
    /// <summary>
    /// 数据对象转换器
    /// </summary>
    public static class DbDataConver
    {
        /// <summary>
        /// 实体类列表转化
        /// </summary>
        /// <param name="DataReader">SqlDataReader</param>
        /// <returns></returns>
        public static List<T> ToList<T>(this IDataReader DataReader) where T : class, new()
        {
            List<T> result = null;
            if (DataReader != null)
            {
                result = new List<T>();
                Type type = typeof(T);
                var method = type.GetProperties();
                var hascolumn = DataReader.GetSchemaTable().Columns;
                while (DataReader.Read())
                {
                    var func = Activator.CreateInstance(type);
                    foreach (PropertyInfo p in method)
                    {
                        if (hascolumn.Contains(p.Name) && DataReader[p.Name] != DBNull.Value)
                        {
                            p.SetValue(func, DataReader[p.Name], null);
                        }
                    }
                    result.Add(func as T);
                }
            }
            return result;

        }
        /// <summary>
        /// 转换单个对象
        /// </summary>
        /// <param name="DataReader">DataReader</param>
        /// <returns></returns>
        public static T ToModel<T>(this IDataReader DataReader) where T : class, new()
        {
            T result = default(T);
            if (DataReader != null)
            {
                Type type = typeof(T);
                var method = type.GetProperties();
                var hascolumn = DataReader.GetSchemaTable().Columns;
                if (DataReader.Read())
                {
                    var func = Activator.CreateInstance(type);
                    foreach (PropertyInfo p in method)
                    {
                        if (hascolumn.Contains(p.Name) && DataReader[p.Name] != DBNull.Value)
                        {
                            p.SetValue(func, DataReader[p.Name], null);
                        }

                    }
                    result = func as T;
                }
            }
            return result;

        }
        /// <summary>
        /// DataTable转成对象
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List<T> ToList<T>(this DataTable table) where T : class, new()
        {
            List<T> result = null;
            Type type = typeof(T);
            if (table.Rows.Count > 0)
            {
                result = new List<T>();
                foreach (DataRow item in table.Rows)
                {
                    var c = Activator.CreateInstance(type);
                    var pro = c.GetType().GetProperties();
                    foreach (PropertyInfo p in pro)
                    {
                        if (table.Columns.Contains(p.Name) && item[p.Name] != DBNull.Value)
                        {
                            p.SetValue(c, item[p.Name], null);
                        }

                    }
                    result.Add(c as T);
                }
            }
            return result;

        }
        /// <summary>
        /// 将objects模型对象转为实体对象
        /// </summary>
        /// <typeparam name="T">实本类</typeparam>
        /// <param name="values">object对象</param>
        /// <returns></returns>
        public static T ToModel<T>(this object values) where T : class, new()
        {
            var result = default(T);
            try
            {
                Type type = typeof(T);
                var members = type.GetProperties();
                var Gtype = values.GetType();
                if (values != null)
                {
                    var func = Activator.CreateInstance(type);
                    foreach (PropertyInfo item in members)
                    {
                        if (Gtype.GetProperty(item.Name) != null)
                        {
                            item.SetValue(func, Gtype.GetProperty(item.Name).GetValue(values));
                        }

                    }
                    result = func as T;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;

        }

    }
}
